package com.hxzy.controller.vue.category;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageInfo;
import com.hxzy.common.enums.AckCode;
import com.hxzy.common.exception.ServiceException;
import com.hxzy.common.resp.PageResp;
import com.hxzy.common.resp.R;
import com.hxzy.common.util.MarketingThreadLocalUtil;
import com.hxzy.common.wrap.PageInfoUtil;
import com.hxzy.controller.vue.category.req.CategoryAddReq;
import com.hxzy.controller.vue.category.req.CategoryEditReq;
import com.hxzy.controller.vue.category.req.CategoryReq;
import com.hxzy.controller.vue.category.resp.CategoryResp;
import com.hxzy.controller.vue.login.resp.AdminLoginResp;
import com.hxzy.controller.vue.mapstruct.CategoryClassConvert;
import com.hxzy.entity.CategoryClass;
import com.hxzy.service.CategoryClassService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Objects;

/**
 * @Author wy
 * @Description 商品分类控制器
 * @Date 2023/9/14 16:39
 * @Version 1.0
 */
@RequestMapping(value = "/api/category")
@RestController
@Api(tags = "后台-商品分类")
public class CategoryController {

    @Autowired
    private CategoryClassService categoryClassService;

    @ApiOperation(value = "分页查询")
    @GetMapping(value = "/data")
    public R<PageResp<CategoryResp>> search( @Valid CategoryReq req){
        //强制为商家赋值
        AdminLoginResp adminLoginResp = MarketingThreadLocalUtil.adminThreadLocal.get();
        if(!adminLoginResp.getPermissions().contains("admin")){
            if(req.getMerchantId()!=null && !req.getMerchantId().equals(adminLoginResp.getId())){
                throw new ServiceException(AckCode.PERMISSION_NOT_ACCESS_CATEGORY);
            }

            req.setMerchantId(adminLoginResp.getId());
        }
        PageInfo<CategoryResp> pageInfo =this.categoryClassService.search(req);
        return R.okHasData(PageInfoUtil.pageInfoToResp(pageInfo));
    }

    @ApiOperation(value = "新增")
    @PostMapping
    public R add(@RequestBody @Valid CategoryAddReq req) {
        CategoryClass entity = new CategoryClass();
        entity.setName(req.getName());
        entity.setPicture(req.getPicture());

        AdminLoginResp adminLoginResp = MarketingThreadLocalUtil.adminThreadLocal.get();
        entity.setMerchantId(adminLoginResp.getId());

        boolean save = this.categoryClassService.save(entity);
        return save?R.ok(): R.build(AckCode.DATABASE_ERROR);
    }

    @ApiOperation(value = "修改")
    @PutMapping
    public R update(@RequestBody @Valid CategoryEditReq req) {
        AdminLoginResp adminLoginResp = MarketingThreadLocalUtil.adminThreadLocal.get();

        CategoryClass db = this.categoryClassService.getById(req.getId());
        if(Objects.isNull(db)){
            return R.build(AckCode.NOT_FOUND_DATA);
        }
        if(!db.getMerchantId().equals(adminLoginResp.getId())){
            return R.build(AckCode.PERMISSION_NOT_ACCESS_CATEGORY);
        }
        db.setName(req.getName());
        db.setPicture(req.getPicture());

        boolean save = this.categoryClassService.updateById(db);
        return save?R.ok(): R.build(AckCode.DATABASE_ERROR);
    }

    @ApiOperation(value = "根据主键查询")
    @GetMapping(value = "/{id}")
    public R<CategoryResp> findById(@PathVariable(value = "id") Long id){
        CategoryClass db = this.categoryClassService.getById(id);
        if(Objects.isNull(db)){
            return R.build(AckCode.NOT_FOUND_DATA);
        }

        CategoryResp convert = CategoryClassConvert.INSTANCE.convert(db);
        return R.okHasData(convert);
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("/{ids}")
    public R delete(@PathVariable(value = "ids") Long[] ids){
        if(ids==null || ids.length==0){
            return R.build(AckCode.SYSTEM_PARAM_FAIL);
        }
        boolean result=this.categoryClassService.deletes(ids);
        return result?R.ok():R.build(AckCode.DATABASE_ERROR);
    }

}
